// pages/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserInfo()
    this.getLabList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getUserInfo() {
    var that = this
    wx.request({
      url: app.globalData.API + '/user',
      data: {},
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function(res) {
        console.log(res.data);
        console.log("[home.js] /user 请求成功", res)
        if (res.data.code == 200) {
          if (app.globalData.isLogin == null)wx.reLaunch({
            url: '../login/login',
          })
          res.data.data.Identity = app.UserIdentity[res.data.data.identity]
          that.setData({
            user: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[home.js] /user 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  labSubmit(e) {
    console.log(e.detail.value)
    if (e.detail.value.name == '')
    {
      wx.showModal({
        title: '提示',
        content: '请完善房间号',
      })
      return
    }
    else
    wx.request({
      url: app.globalData.API + '/lab',
      data: e.detail.value,
      method: "post",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res.data);
        console.log("[home.js] /user 请求成功", res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.message,
 
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[home.js] /user 请求失败", res.data)
      },
      complete: res => {

      }
    })
  }, equSubmit(e){
    if (e.detail.value.name == '') {
      wx.showModal({
        title: '提示',
        content: '请完善房间号',
      })
      return
    }
    else
      wx.request({
        url: app.globalData.API + '/equipment',
        data: e.detail.value,
        method: "post",
        header: {
          'cookie': wx.getStorageSync("sessionid"), //读取cookie
          "content-type": "application/json"
        },
        success: function (res) {
          console.log(res.data);
          console.log("[home.js] /user 请求成功", res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.message,

            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
            })
          }
        },
        fail: res => {
          console.log("[home.js] /user 请求失败", res.data)
        },
        complete: res => {

        }
      })
  },
  toSubscribe(){
    wx.navigateTo({
      url: '../subscribe/subscribe',
    })
  },
  toadminSubscribe() {
    wx.navigateTo({
      url: '../subscribe/adminsubscribe',
    })
  },
  toBorrow() {
    wx.navigateTo({
      url: '../borrow/borrow',
    })
  },
  toadminBorrow() {
    wx.navigateTo({
      url: '../borrow/adminborrow',
    })
  },
  toMine(){
    wx.navigateTo({
      url: '../mine/mine',
    })
  },
  getLabList() {
    var that = this
    wx.request({
      url: app.globalData.API + '/lab/borrow/list',
      data: {
        // examine_status: 0
      },
      method: "get",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log("[subscribe.js] /lab/borrow/list 请求成功", res)
        if (res.data.code == 200) {
          that.setData({
            labList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/borrow/list 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  accept(e){
    console.log(e.target.dataset.id)
    var that = this
    wx.request({
      url: app.globalData.API + '/lab/subscribe',
      data: {
        id: e.target.dataset.id,
        examine_status: 1
      },
      method: "put",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log("[subscribe.js] /lab/subscribe 请求成功", res)
        if (res.data.code == 200) {
          that.onLoad()
          wx.showToast({
            title: res.data.message,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/subscribe 请求失败", res.data)
      },
      complete: res => {

      }
    })
  },
  reject(e){

    var that = this
    wx.request({
      url: app.globalData.API + '/lab/subscribe',
      data: {
        id: e.target.dataset.id,
        examine_status: 2
      },
      method: "put",
      header: {
        'cookie': wx.getStorageSync("sessionid"), //读取cookie
        "content-type": "application/json"
      },
      success: function (res) {
        console.log("[subscribe.js] /lab/subscribe 请求成功", res)
        if (res.data.code == 200) {
          that.onLoad()
          wx.showToast({
            title: res.data.message,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: res => {
        console.log("[subscribe.js] /lab/subscribe 请求失败", res.data)
      },
      complete: res => {

      }
    })

  },
  unlogin(){
    wx.reLaunch({
      url: '../login/login',
    })
  }

})